import proto from "../proto/proto";
import { ClientConnection } from "../socket/connection/ClientConnection";

/**
 * 玩家基类
 */
export abstract class PlayerBase {
    protected _userid: number = 0;
    protected _offline: boolean = false;
    get userId() {
        return this._userid;
    }
    get offline() {
        return this._offline;
    }
    protected _connection: ClientConnection | null = null;
    get connection() {
        return this._connection;
    }

    constructor(userId: number) {
        this._userid = userId;
    }

    public setConnection(connection: ClientConnection) {
        this._connection = connection;
        this._connection.userId = this._userid;
    }

    /**
     * 发送消息
     */
    sendData(cmd: number, reqWrap: proto.ReqWrap, code: proto.CODE, data: any = {}) {
        if (this._connection)
            this._connection.sendData(cmd, reqWrap, code, data);
    }

    /**
     * 设置掉线
     */
    setOffline() {
        this._offline = true;
        this._connection = null
    }

    /**
     * 设置在线
     * @param connection 
     */
    setOnline(connection: ClientConnection) {
        this._offline = false
        this._connection = connection;
    }
}